package com.bq.server.controller;

import com.bq.server.base.BaseController;
import com.bq.server.base.Result;
import com.bq.server.core.model.CadreAppointInfo;
import com.bq.server.core.model.Department;
import com.bq.server.core.vo.request.DepartmentRequest;
import com.bq.server.core.vo.response.DeptAndCadreInfoResponse;
import com.bq.server.service.DepartmentService;
import com.bq.server.tree.TreeNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ChenSH
 * @date 2021年10月14日 11:05 上午
 */
@Slf4j
@RestController
@RequestMapping("/admin/departments")
@RequiredArgsConstructor
public class DepartmentController extends BaseController {

    private final DepartmentService departmentService;


    /**
     * 部门树
     */
    @GetMapping("/trees")
    public Result<List<TreeNode<Department>>> tree() {
        List<TreeNode<Department>> tree = departmentService.getDepartmentTrees();
        return success(tree);
    }

    /**
     * 部门/人员搜索
     */
    @GetMapping("/search")
    public Result<DeptAndCadreInfoResponse> searchDeptAndCadres(String search) {
        DeptAndCadreInfoResponse response = departmentService.searchDeptAndCadres(search);
        return success(response);
    }

    /**
     * 删除部门
     */
    @DeleteMapping("/{id}")
    public Result<Void> remove(@PathVariable Long id) {
        departmentService.remove(id);
        return success();
    }

    /**
     * 查询部门下的干部信息
     */
    @GetMapping("/{id}/cadres")
    public Result<List<CadreAppointInfo>> getCadres(@PathVariable Long id) {
        List<CadreAppointInfo> cadreAppointInfoList = departmentService.getCadres(id);
        return success(cadreAppointInfoList);
    }

    /**
     * 创建部门
     *
     * @return 成功/失败
     */
    @PostMapping("/create")
    public Result<Void> create(@RequestBody DepartmentRequest request) {
        departmentService.create(request);
        return Result.success();
    }

    /**
     * 创建部门
     *
     * @return 成功/失败
     */
    @PutMapping("/{id}/update-name")
    public Result<Void> updateName(@PathVariable Long id,
                                   @RequestBody @Validated DepartmentRequest request) {
        departmentService.updateNameById(id, request);
        return Result.success();
    }
}
